<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables, $debug;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

bigtitle($txt['bnt_warp_title']);

//-------------------------------------------------------------------------------------------------

if ($debug)
{
	echo '<pre>';
	print_r($_POST);
	echo '</pre>';
}

//-------------------------------------------------------------------------------------------------

// Pull player info into $playerinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Run checks to ensure player enough turns and warp edit devices.
if ($playerinfo['turns'] < 1)
{
	echo $txt['bnt_warp_turn'], '<br><br>';
	TextMainMenu();
	return;
}

if ($playerinfo['dev_warpedit'] < 1)
{
	echo $txt['bnt_warp_none'], '<br><br>';
	TextMainMenu();
	return;
}

// Run check to ensure player can edit warp links in this sector.
$result = $smcFunc['db_query']('', '
	SELECT allow_warpedit, {raw:db_universe}.zone_id
	FROM {raw:db_zones}, {raw:db_universe}
	WHERE sector_id = {int:sector}
	AND {raw:db_universe}.zone_id = {raw:db_zones}.zone_id',
	array(
		'db_universe' => $dbtables['universe'],
		'db_zones' => $dbtables['zones'],
		'sector' => $playerinfo['sector'],
	)
);
$zoneinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($zoneinfo['allow_warpedit'] == 'N')
{
	echo $txt['bnt_warp_forbid'], '<br><br>';
	TextMainMenu();
	return;
}

// Player is able to edit warp links, so read the post variables
if (isset($_POST['target_sector']))
{
	$target_sector = round($_POST['target_sector']);
	$oneway = (isset($_POST['bothway'])) ? $_POST['bothway'] : '';
}
else
{
	echo $txt['bnt_warp_nosector'], '<br><br>';
	TextMainMenu();
	return;
}

$result = $smcFunc['db_query']('', '
	SELECT allow_warpedit, {raw:db_universe}.zone_id
	FROM {raw:db_zones}, {raw:db_universe}
	WHERE sector_id = {int:sector}
	AND {raw:db_universe}.zone_id = {raw:db_zones}.zone_id',
	array(
		'db_universe' => $dbtables['universe'],
		'db_zones' => $dbtables['zones'],
		'sector' => $target_sector,
	)
);

$zoneinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($zoneinfo['allow_warpedit'] == 'N' && !empty($bothway))
{
	$txt['bnt_warp_forbidtwo'] = str_replace("[target_sector]", $target_sector, $txt['bnt_warp_forbidtwo']);
	echo $txt['bnt_warp_forbidtwo'], '<br><br>';
	TextMainMenu();
	return;
}

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_universe}
	WHERE sector_id = {int:target_sector}',
	array(
		'db_universe' => $dbtables['universe'],
		'target_sector' => $target_sector
	)
);

if ($smcFunc['db_num_rows']($result) == 0)
{
	echo $txt['bnt_warp_nosector'], '<br><br>';
	TextMainMenu();
	return;
}
$smcFunc['db_free_result']($result);

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_links}
	WHERE link_start = {int:sector}',
	array(
		'db_links' => $dbtables['links'],
		'sector' => $playerinfo['sector']
	)
);

$flag = 0;

if ($smcFunc['db_num_rows']($result) > 0)
{
	while($row = $smcFunc['db_fetch_assoc']($result))
	{
		if ($target_sector == $row['link_dest'])
			$flag = 1;
	}
	if ($flag != 1)
	{
		$txt['bnt_warp_unlinked'] = str_replace("[target_sector]", $target_sector, $txt['bnt_warp_unlinked']);
		echo $txt['bnt_warp_unlinked'], '<br><br>';
	}
	else
	{
		$delete = $smcFunc['db_query']('', '
			DELETE FROM {raw:db_links}
			WHERE link_start = {int:sector}
			AND link_dest = {int:target_sector}',
			array(
				'db_links' => $dbtables['links'],
				'sector' => $playerinfo['sector'],
				'target_sector' => $target_sector
			)
		);
		
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET dev_warpedit = dev_warpedit - 1, 
				turns = turns - 1, 
				turns_used = turns_used + 1
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $playerinfo['ship_id']
			)
		);

		if (empty($bothway))
			echo $txt['bnt_warp_removed'], '  ', $target_sector, '<br><br>';
		else
		{
			$delete = $smcFunc['db_query']('', '
				DELETE FROM {raw:db_links}
				WHERE link_start = {int:target_sector}
				AND link_dest = {int:sector}',
				array(
					'db_links' => $dbtables['links'],
					'target_sector' => $target_sector,
					'sector' => $playerinfo['sector']
				)
			);
			echo $txt['bnt_warp_removedtwo'], '  ', $target_sector, '<br><br>';
		}
	}
}

TextMainMenu();
?>
